package com.jwx.mycar.controller;

import com.jwx.mycar.entity.po.Reply;
import com.jwx.mycar.entity.vo.ResultVo;
import com.jwx.mycar.service.ReplyService;
import com.jwx.mycar.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.event.MailEvent;
import java.util.List;

/**
 * @description:添加回复
 * @author: jwx
 * @time: 2019/11/27 17:44
 */
@Api(tags = "回复相关")
@RestController
@RequestMapping("/reply")
public class ReplyController {
    @Autowired
    ReplyService replyService;
    @Autowired
    ResultVOUtil resultVOUtil;

    @ApiOperation(value = "添加回复" ,notes = "如果是一级回复，则将回复的parentId设置为null")
    @PostMapping("/add")
            public ResultVo add(@RequestBody Reply reply){
           boolean isSuccess =  replyService.add(reply);
            if(isSuccess==true){
                return resultVOUtil.success(reply);
            }
            else {
                return resultVOUtil.fail("回复失败");
            }

    }
    /**
     *
     * @description: 根据id删除回复
     * @param null
     * @return: 
     * @author: jwx
     * @time: 2019/12/6 20:58
     */    
    
    @ApiOperation(value = "根据id删除回复",notes = "前端传入回复的主键")
    @PostMapping(value = "/delete",params = {"replyId"})
    public ResultVo delete(@RequestParam("replyId") String replyId){
         boolean isSuccess = replyService.deleteById(replyId);
         if(isSuccess){
             return resultVOUtil.success(replyId);
         }
         else {
             return resultVOUtil.fail("删除失败");
         }
    }
    /**
     *
     * @description: 根据id获取所有帖子
     * @param null
     * @return: 
     * @author: jwx
     * @time: 2019/12/6 20:56
     */    
    
    @ApiOperation(value = "根据帖子id获取所有回复",notes = "返回的回复列表根据parentId分类")
    @ApiImplicitParam(name = "draftId", value = "帖子id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/getAllReply")
    public ResultVo getAllReply(@RequestParam("draftId") String draftId){

      List<Reply>  replyList = replyService.getAll(draftId);
      if (replyList==null){
          return resultVOUtil.fail("draftId不存在或者没有评论");
      }
      else {
          return  resultVOUtil.success(replyList);
      }
    }
}
